// components/search/module/sCoach/sCoach.js
const { debounce } = require("../../../../utils/util");
Component({
  options: {
    // virtualHost: true,
    styleIsolation: "apply-shared",
  },
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: () => {
        return {};
      },
    },
    value: {
      type: String | Number,
      value: "",
    },
    show: {
      type: Boolean,
      value: false,
    },
  },
  observers: {
    data(v) {
      this.setData({
        list: v.list ? [...v.list] : [],
      });
    },
    value(v) {
      this.setData({
        insideValue: v
          ? this.data.data.list.find((i) => i.coach_id === v).nickname
          : "",
        pValue: v || 0,
      });
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    insideValue: "",
    pValue: 0,
    list: [],
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.triggerEvent("close");
    },
    onSearch: debounce(function(e){
      this.setData({
        list: this.data.data.list.filter(
          (i) => i.nickname.indexOf(e.detail.value) !== -1
        ),
      });
    }, 500),
    changeVal(e) {
      this.setData({
        pValue: this.data.list[e.currentTarget.dataset.index].coach_id,
      });
    },
    getValue(e) {
      this.setData({
        insideValue: this.data.list.find(i => i.coach_id === this.data.pValue).nickname,
        show: false,
      });
      this.triggerEvent("value", {
        type: this.data.data.name,
        value: this.data.pValue,
      });
    },
  },
});
